$(function() {
//Declare variable
var tips = $( ".validateTips" ),
	button_name="",
	url="a_cancelbill.php", 
	ID="",
	AddTitle="Cancel Bill ",	
	rowSelect="";
		
		ID=$("#billno").val();
		
		//load first page
		LoadData(1,false);
		
		//Show Loading bar			
		function showLoader(){
			$( "#resultData tbody:first" ).prepend( "<tr>"
						+"<td colspan='8' class='search-background'>"
						+"<label><img src='images/ajax-loader.gif'/></label>"
						+"</td>"
						+"</tr>" ); 
			 $('.search-background').fadeIn(900,0);
		}
		//Hide Loading bar			
		function hideLoader(){
			$('.search-background').fadeOut(300);
			$( "#resultData tbody tr" ).remove();
		};		
				
	
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		//Confirm Dialog
		$( "#dialog-confirm" ).dialog({
			resizable: false,
			height:160,
			zIndex:9999,
			autoOpen: false,
			modal: true
		});
		

		function setValue(obj){
			$("#name").val(obj.eq(2).html());
			$("#tAmount").val(obj.eq(5).html());
			$("#rAmount").val(obj.eq(7).html());
			
			 ID=obj.eq(1).html();
			
			var dataSet={'id':ID,'m':'detail'};  
			//alert(id);
			 $.ajax({
				type: 'POST',
				cache: false,
				url: url,
				data: dataSet,
				success: function(data){
					//	alert(data);
					var obj=eval('('+data+')');
					
					$("#tbPayHistory tbody:first" ).prepend( obj['html'] ); 
					$("#iCount").attr('innerHTML',obj['rows']);	
					$("#dlgTotalPay").attr('innerHTML',obj['paytoal']);	
					
				}
			});
		}
	
	//Cancel bill
	$("#btnConfirm").live('click',function(){
		var varsID="";
		var i=0;
		var rows=$("#rowCount").attr('innerHTML');
		var isChkAll=1;
		$("input[name=chkCancel]:checked").each(function(){
			varsID += $(this).val() +",";
			//allVars.push($(this).val());
			i++;
		});
		if (i==0){
			alert("Please select row to cancel bill");
			return;
		}
		$("#dialog-confirm").dialog("open");
		$("#dialog-confirm").dialog("option","buttons",	{
			"Cancel Bill": function() {
					

			//	alert(varsID);
				//Select all or not
				if (rows!=i)
					isChkAll=0;	
					
				 $.ajax({
					type: 'POST',
					cache: false,
					url: url,
					data: {'m':'cancel_detail','id':ID, 'var':varsID,'IsAll':isChkAll},
					success: function(data){
						alert("completed");
					}
				});
				$( this ).dialog( "close" );
				history.back(-1);
			},
			Close: function() {
				
				$( this ).dialog( "close" );
			}
		});
	});
	// $("input[name=chkAll]").click(function(){
		// if ($(this).attr('checked')==true){
			// $("input[name=chkCancel]").attr('checked','check');
		// }else{
			// $("input[name=chkCancel]").removeAttr('checked');
		// }
	// });
	
	$("#btnBack").live('click',function(){
		$("#Act").attr("action", "cancelbill.php");
		$("#Act").submit();
		//Acthistory.back(-1);
	});
	
	function LoadData(page,b){
			$( "#resultData tbody tr" ).remove();
			//Show loading
			showLoader();
			//set criteria
			 
			var dataSet={'m':'detail'
						,'id':ID
						};
					
			 //Post 		
			 $.post(url,  dataSet , function(data){
			 // alert(data);
				hideLoader();
				//convert string json to java json
				var obj=eval("(" + data + ")");
				var m=eval("("+obj['header']+")");
				var p=obj['paytype']; 
				 
				$("#iCode").attr('innerHTML', m['Bill']);
				$("#lblName").attr('innerHTML',m['Name'] );
				$("#lblSaleDate").attr('innerHTML',m['SaleDate1'] );
				$("#lblPayType").attr('innerHTML',p);
				$("#iAmount").attr('innerHTML',CommaFormatted(m['Amount']));
				$("#iDiscount").attr('innerHTML',CommaFormatted(m['Discount']));
				$("#iTotal").attr('innerHTML', CommaFormatted(m['Total']));				
				 
				//set data to table
				$( "#resultData tbody" ).append(obj['data']);
				//set row 
				$("#rowCount").attr('innerHTML',obj['rows']);
				//set page
			if (b){
 
					if (parseInt(obj['totalpage'])>1){
						$("#paging_btn").show();
						setPaging(parseInt(obj['totalpage']));
					}else{
						$("#paging_btn").hide();
					}					
					 
				}
			 });
		}
 
			
	
	function setPaging(iTotal){
				 $("#paging_btn").paginate({
				count 		: iTotal,
				start 		: 1,
				display     : 10,
				border					: false,
				text_color  			: '#888',
				background_color    	: '#EEE',	
				text_hover_color  		: 'blue',
				background_hover_color	: '#CFCFCF',
				mouse					: 'press',
				onChange     			: function(page){
											LoadData(page,false);
										  }
			});
		}
		
	function CommaFormatted(amount)
	{
	 
		var delimiter = ","; // replace comma if desired
		var a = amount.split('.',2)
		var d = a[1];
		var i = parseInt(a[0]);
		if(isNaN(i)) { return ''; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		var n = new String(i);
		var a = [];
		while(n.length > 3)
		{
			var nn = n.substr(n.length-3);
			a.unshift(nn);
			n = n.substr(0,n.length-3);
		}
		if(n.length > 0) { a.unshift(n); }
		n = a.join(delimiter);
		if(d.length < 1) { amount = n; }
		else { amount = n + '.' + d; }
		amount = minus + amount;
		return amount;
	}
});
	